#include "StdAfx.h"
#include "MapLayerMgr.h"
#include "MapLayer.h"

CMapLayerMgr::CMapLayerMgr()
{
}

CMapLayerMgr::~CMapLayerMgr()
{
    if (m_mapLayers.GetSize() != 0)
    {
        for (int ii = 0; ii < m_mapLayers.GetSize(); ii++)
        {
            CMapLayer* mayLayer = m_mapLayers.GetAt(ii);
            delete mayLayer;
        }

        m_mapLayers.RemoveAll();
    }
}

void CMapLayerMgr::addMapLayer(CMapLayer* pMapLayer)
{
    if (!pMapLayer)
    {
        return;
    }
	if (m_mapLayers.GetSize() == 0)
	{
		m_maxEnvelope = pMapLayer->getLayerEnvelope();
	}
    CMapLayerDrawProperties properties;
    properties.m_rColor = rand()%255;
    properties.m_gColor = rand()%255;
    properties.m_bColor = rand()%255;
    properties.m_width = 2;
    pMapLayer->setDrawProperties(properties);
    m_mapLayers.Add(pMapLayer);
    MapLayerAction action;
    action.actionType = MapLayerActionType::Add;
    action.m_mapLayers.Add(pMapLayer);

    //for (int ii = 0; ii < m_eventPublisher.m_enventHandlers.GetCount(); ii++)
    //{
    //    IEventHandler<CMapLayerMgr* ,MapLayerAction*>* handler = m_eventPublisher.m_enventHandlers.GetAt(ii);
    //    if (handler)
    //    {
    //        handler->HandleEvent(this,&action);
    //    }
    //}
    m_eventPublisher.InvokeHandler(this,&action);
}

void CMapLayerMgr::deleteMapLayer(CMapLayer* pMapLayer)
{
    for (int ii = 0;ii < m_mapLayers.GetCount();ii++)
    {
        if (pMapLayer == m_mapLayers.GetAt(ii))
        {
            m_mapLayers.RemoveAt(ii);
        }
    }
}